package cn.com.dcsgo.security;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.annotation.Resource;

/**
 * 登录拦截器配置
 *
 * @author Dcsgo
 * @since 2025/4/24 15:18
 */
@Configuration
public class LoginMvcConfigurerAdapter extends WebMvcConfigurationSupport {
    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        //拦截除登录接口以外的所有接口
        registry.addInterceptor(new UserLoginInterceptor(redisTemplate))
                .addPathPatterns("/weixin/**")
                // 排除登录接口和 Swagger 文档相关路径
                .excludePathPatterns(
                        "/weixin/user/login/*",
                        "/doc.html",
                        "/webjars/**",
                        "/swagger-resources/**",
                        "/v2/api-docs"
                );
        super.addInterceptors(registry);
    }

    /**
     * 继承 WebMvcConfigurationSupport 会覆盖 Spring Boot 的默认静态资源映射
     * 如果未手动配置 Swagger 静态资源路径，会导致 /doc.html 无法访问
     *
     * @param registry registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置 Swagger 的静态资源
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

}
